package com.campus.client.controller;

import com.campus.client.net.ClientConnector;
import com.campus.common.message.StudentCourseRequest;
import com.campus.common.message.StudentCourseResponse;
import com.campus.common.vo.Course;

import java.io.IOException;
import java.util.List;

public class StudentCourseController {
    private ClientConnector connector;

    public StudentCourseController(ClientConnector connector) {
        this.connector = connector;
    }
    // 查看所有课程（无论是否可选）
    public List<Course> viewAllCourses() throws IOException, ClassNotFoundException {
        StudentCourseRequest req = new StudentCourseRequest();
        req.setAction(StudentCourseRequest.Action.VIEW_ALL_COURSES);

        StudentCourseResponse resp = connector.sendStudentCourseRequest(req);
        return resp.getCourses();
    }
    // 查看可选课程（每个课程包含班级列表）
    public List<Course> viewAvailableCourses(String studentId) throws IOException, ClassNotFoundException {
        StudentCourseRequest req = new StudentCourseRequest();
        req.setAction(StudentCourseRequest.Action.VIEW_AVAILABLE_COURSES);
        req.setStudentId(studentId);

        StudentCourseResponse resp = connector.sendStudentCourseRequest(req);
        return resp.getCourses();
    }

    // 查看已选课程
    public List<Course> viewSelectedCourses(String studentId) throws IOException, ClassNotFoundException {
        StudentCourseRequest req = new StudentCourseRequest();
        req.setAction(StudentCourseRequest.Action.VIEW_SELECTED_COURSES);
        req.setStudentId(studentId);

        StudentCourseResponse resp = connector.sendStudentCourseRequest(req);
        return resp.getCourses();
    }

    // 选课
    public String selectCourse(String studentId, String classId) throws IOException, ClassNotFoundException {
        StudentCourseRequest req = new StudentCourseRequest();
        req.setAction(StudentCourseRequest.Action.SELECT_CLASSROOM);
        req.setStudentId(studentId);
        req.setClassId(classId);

        StudentCourseResponse resp = connector.sendStudentCourseRequest(req);
        return resp.getMessage();
    }

    // 退课
    public String dropCourse(String studentId, String classId) throws IOException, ClassNotFoundException {
        StudentCourseRequest req = new StudentCourseRequest();
        req.setAction(StudentCourseRequest.Action.DROP_CLASSROOM);
        req.setStudentId(studentId);
        req.setClassId(classId);

        StudentCourseResponse resp = connector.sendStudentCourseRequest(req);
        return resp.getMessage();
    }

    // 查询课程
    public List<Course> queryCourses(String keyword) throws IOException, ClassNotFoundException {
        StudentCourseRequest req = new StudentCourseRequest();
        req.setAction(StudentCourseRequest.Action.QUERY_COURSE);
        req.setKeyword(keyword);


        StudentCourseResponse resp = connector.sendStudentCourseRequest(req);
        return resp.getCourses();
    }

    // 查看首修课程
    public List<Course> viewFirstTimeCourses(String studentId) throws IOException, ClassNotFoundException {
        StudentCourseRequest req = new StudentCourseRequest();
        req.setAction(StudentCourseRequest.Action.VIEW_FIRST_TIME_COURSES);
        req.setStudentId(studentId);

        StudentCourseResponse resp = connector.sendStudentCourseRequest(req);
        return resp.getCourses();
    }

    // 查看重修课程
    public List<Course> viewRetakeCourses(String studentId) throws IOException, ClassNotFoundException {
        StudentCourseRequest req = new StudentCourseRequest();
        req.setAction(StudentCourseRequest.Action.VIEW_RETAKE_COURSES);
        req.setStudentId(studentId);

        StudentCourseResponse resp = connector.sendStudentCourseRequest(req);
        return resp.getCourses();
    }

    // 可选：关闭连接
    public void closeConnection() throws IOException {
        connector.close();
    }
}
